home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / cxref-1.001 / cxref-1~ / cxref / func.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-24  |  8.7 KB  |  352 lines

  1. /***************************************
  2.   $Header: /home/amb/cxref/RCS/func.c 1.9 1996/02/24 14:53:12 amb Exp $
  3.  
  4.   C Cross Referencing & Documentation tool. Version 1.0
  5.  
  6.   Handle Function stuff.
  7.   ******************/ /******************
  8.   Written by Andrew M. Bishop
  9.  
  10.   This file Copyright 1995,96 Andrew M. Bishop
  11.   It may be distributed under the GNU Public License, version 2, or
  12.   any higher version.  See section COPYING of the GNU Public license
  13.   for conditions under which this file may be redistributed.
  14.   ***************************************/
  15.  
  16. /*+ Control the debugging information from this file. +*/
  17. #define DEBUG 0
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. #include "memory.h"
  24. #include "datatype.h"
  25. #include "cxref.h"
  26.  
  27. /*+ The current parsing options. +*/
  28. extern int option_xref;
  29.  
  30. /*+ The current file that is being processed. +*/
  31. extern File CurFile;
  32.  
  33. /*+ The current function, this is initialised by the start of a possible declaration and maintained until all of the
  34.     arguments have been added and confimation that it is a definition and not a prototype is seen. +*/
  35. static Function cur_func=NULL;
  36.  
  37. /*+ The name of the current file. +*/
  38. static char* cur_cpp_file=NULL;
  39.  
  40. /*+ The list of function prototypes and the files that they are defined in. +*/
  41. static StringList2 prototypes={0,NULL,NULL};
  42.  
  43. /*++++++++++++++++++++++++++++++++++++++
  44.   Function that is called when a #include directive is seen in the current file.
  45.  
  46.   char* SeenCPPFilename Returns a pointer to a copy of the filename.
  47.  
  48.   char* name The name of the included file.
  49.   ++++++++++++++++++++++++++++++++++++++*/
  50.  
  51. char* SeenCPPFilename(char* name)
  52. {
  53. #if DEBUG
  54.  printf("#Func.c# CPP Filename '%s'\n",name);
  55. #endif
  56.  
  57.  if(name[0]=='.' && name[1]=='/')
  58.     cur_cpp_file=CopyString(&name[2]);
  59.  else
  60.     cur_cpp_file=CopyString(name);
  61.  
  62.  return(cur_cpp_file);
  63. }
  64.  
  65.  
  66. /*++++++++++++++++++++++++++++++++++++++
  67.   Function that is called when a function prototype is seen.
  68.  
  69.   char* name The name of the function.
  70.   ++++++++++++++++++++++++++++++++++++++*/
  71.  
  72. void SeenFunctionProto(char* name)
  73. {
  74. #if DEBUG
  75.  printf("#Func.c# Function Prototype '%s'\n",name);
  76. #endif
  77.  
  78.  AddToStringList2(&prototypes,name,cur_cpp_file);
  79. }
  80.  
  81.  
  82. /*++++++++++++++++++++++++++++++++++++++
  83.   Function that is called when a function declaration is seen. This may or may not be a function defintion, we will need to wait and see.
  84.  
  85.   char* name The name of the function.
  86.  
  87.   int scope The scope of the function definition.
  88.   ++++++++++++++++++++++++++++++++++++++*/
  89.  
  90. void SeenFunctionDeclaration(char* name,int scope)
  91. {
  92.  Function func=CurFile->functions;
  93.  
  94. #if DEBUG
  95.  printf("#Func.c# Function declaration for '%s()'\n",name);
  96. #endif
  97.  
  98.  while(func)
  99.    {
  100.     if(cur_func==func) {cur_func=NULL;break;}
  101.     func=func->next;
  102.    }
  103.  
  104.  if(cur_func)
  105.    {
  106.     if(cur_func->comment) Free(cur_func->comment);
  107.     if(cur_func->name)    Free(cur_func->name);
  108.     if(cur_func->type)    Free(cur_func->type);
  109.     DeleteStringList2(&cur_func->args,1);
  110.     if(cur_func->cret)    Free(cur_func->cret);
  111.     DeleteStringList(&cur_func->calls);
  112.     DeleteStringList(&cur_func->v_refs);
  113.     DeleteStringList(&cur_func->f_refs);
  114.    }
  115.  else
  116.     cur_func=(Function)Malloc(sizeof(struct _Function));
  117.  
  118.  cur_func->comment=MallocString(GetCurrentComment());
  119.  cur_func->name=MallocString(name);
  120.  cur_func->protofile=NULL;
  121.  cur_func->scope=scope;
  122.  cur_func->type=NULL;
  123.  InitStringList2(&cur_func->args);
  124.  cur_func->cret=NULL;
  125.  InitStringList(&cur_func->calls);
  126.  InitStringList(&cur_func->called);
  127.  InitStringList(&cur_func->used);
  128.  InitStringList(&cur_func->v_refs);
  129.  InitStringList(&cur_func->f_refs);
  130.  cur_func->next=NULL;
  131. }
  132.  
  133.  
  134. /*++++++++++++++++++++++++++++++++++++++
  135.   Called when a possible function definition is confirmed.
  136.  
  137.   char* type The type of the function.
  138.   ++++++++++++++++++++++++++++++++++++++*/
  139.  
  140. void SeenFunctionDefinition(char* type)
  141. {
  142.  Function *func=&CurFile->functions;
  143.  int i;
  144.  
  145. #if DEBUG
  146.  printf("#Func.c# Function definition for '%s()'\n",cur_func->name);
  147. #endif
  148.  
  149.  cur_func->type=MallocString(type);
  150.  
  151.  cur_func->cret=SplitComment(&cur_func->comment,type);
  152.  if(!cur_func->cret)
  153.     cur_func->cret=MallocString(GetCurrentComment());
  154.  
  155.  if(option_xref&XREF_FUNC)
  156.     for(i=0;i<prototypes.n;i++)
  157.        if(!strcmp(cur_func->name,prototypes.s1[i]))
  158.          {cur_func->protofile=MallocString(prototypes.s2[i]); break;}
  159.  
  160.  while(*func)
  161.    {
  162.     if(strcmp(cur_func->name,(*func)->name)<0)
  163.       {
  164.        Function temp=*func;
  165.        *func=cur_func;
  166.        cur_func->next=temp;
  167.        break;
  168.       }
  169.     func=&(*func)->next;
  170.    }
  171.  
  172.  if(!cur_func->next)
  173.     *func=cur_func;
  174.  
  175. }
  176.  
  177. /*++++++++++++++++++++++++++++++++++++++
  178.   Function that is called when a function argument is seen in the current function declaration.
  179.  
  180.   char* name The argument.
  181.   ++++++++++++++++++++++++++++++++++++++*/
  182.  
  183. void SeenFunctionArg(char* name)
  184. {
  185. #if DEBUG
  186.  printf("#Func.c# Function arg '%s' in %s()\n",name,cur_func->name);
  187. #endif
  188.  
  189.  AddToStringList2(&cur_func->args,MallocString(name),SplitComment(&cur_func->comment,name));
  190.  if(!cur_func->args.s2[cur_func->args.n-1])
  191.     cur_func->args.s2[cur_func->args.n-1]=MallocString(GetCurrentComment());
  192. }
  193.  
  194.  
  195. /*++++++++++++++++++++++++++++++++++++++
  196.   Function that is called when a comment is seen in a function body.
  197.  
  198.   char* comment The comment that has been seen.
  199.   ++++++++++++++++++++++++++++++++++++++*/
  200.  
  201. void SeenFuncIntComment(char* comment)
  202. {
  203. #if DEBUG
  204.  printf("#Func.c# Function internal comment '%s' in %s()\n",comment,cur_func->name);
  205. #endif
  206.  
  207.  if(cur_func->comment)
  208.    {
  209.     char* c=cur_func->comment;
  210.  
  211.     cur_func->comment=MallocString(ConcatStrings(3,c,"\n\n",comment));
  212.     Free(c);
  213.    }
  214.  else
  215.     cur_func->comment=MallocString(comment);
  216. }
  217.  
  218.  
  219. /*++++++++++++++++++++++++++++++++++++++
  220.   Function that is called when a function call is seen in the current function.
  221.  
  222.   char* name The name of the function that is called.
  223.   ++++++++++++++++++++++++++++++++++++++*/
  224.  
  225. void SeenFunctionCall(char* name)
  226. {
  227.  if(!option_xref&XREF_FUNC)
  228.     return;
  229.  
  230. #if DEBUG
  231.  printf("#Func.c# Function call for '%s()' in %s()\n",name,cur_func->name);
  232. #endif
  233.  
  234.  AddToStringList(&cur_func->calls,name,1);
  235. }
  236.  
  237.  
  238. /*++++++++++++++++++++++++++++++++++++++
  239.   Function that is called when a function or variable is referenced in the current function.
  240.  
  241.   char* name The name of the function or variable that is referenced.
  242.  
  243.   int in_a_function Whether the reference is from within a function or at the top level of the file.
  244.   ++++++++++++++++++++++++++++++++++++++*/
  245.  
  246. void CheckFunctionVariableRef(char* name,int in_a_function)
  247. {
  248.  Variable var =CurFile->variables;
  249.  Function func=CurFile->functions;
  250.  StringList *sl=NULL;
  251.  
  252.  if(!option_xref&(XREF_VAR|XREF_FUNC))
  253.     return;
  254.  
  255.  if(IsAScopeVariable(name))
  256.     return;
  257.  
  258. #if DEBUG
  259.  printf("#Func.c# Function/Variable reference for '%s' in %s\n",name,in_a_function?cur_func->name:CurFile->name);
  260. #endif
  261.  
  262.  if(option_xref&XREF_VAR)
  263.     while(var)
  264.       {
  265.        if(!strcmp(var->name,name))
  266.          {
  267.           if(in_a_function)
  268.              sl=&cur_func->v_refs;
  269.           else
  270.              sl=&CurFile->v_refs;
  271.          }
  272.        var=var->next;
  273.       }
  274.  
  275.  if(!sl && option_xref&XREF_FUNC)
  276.     while(func)
  277.       {
  278.        if(!strcmp(func->name,name))
  279.          {
  280.           if(in_a_function)
  281.              sl=&cur_func->f_refs;
  282.           else
  283.              sl=&CurFile->f_refs;
  284.          }
  285.        func=func->next;
  286.       }
  287.  
  288.  if(!sl && option_xref&XREF_FUNC)
  289.    {
  290.     int i;
  291.     for(i=0;i<prototypes.n;i++)
  292.        if(!strcmp(name,prototypes.s1[i]))
  293.          {
  294.           if(in_a_function)
  295.              sl=&cur_func->f_refs;
  296.           else
  297.              sl=&CurFile->f_refs;
  298.          }
  299.    }
  300.  
  301.  /* Now add the function or variable to the Function / File structure. */
  302.  
  303.  if(sl)
  304.     AddToStringList(sl,name,1);
  305. }
  306.  
  307.  
  308. /*++++++++++++++++++++++++++++++++++++++
  309.   Clears the memory that is used here.
  310.   This is the list of function prototypes and the cur_func variable if not already empty.
  311.   ++++++++++++++++++++++++++++++++++++++*/
  312.  
  313. void DeleteSpareProtos(void)
  314. {
  315.  cur_cpp_file=NULL;
  316.  
  317.  DeleteStringList2(&prototypes,0);
  318.  InitStringList2(&prototypes);
  319.  
  320.  if(cur_func)
  321.    {
  322.     Function func=CurFile->functions;
  323.     int delete_cur_func=1;
  324.  
  325.     while(func)
  326.       {
  327.        if(func==cur_func)
  328.           delete_cur_func=0;
  329.  
  330.        func=func->next;
  331.       }
  332.  
  333.     if(delete_cur_func)
  334.       {
  335.        if(cur_func->comment)   Free(cur_func->comment);
  336.        Free(cur_func->name);
  337.        if(cur_func->type)      Free(cur_func->type);
  338.        if(cur_func->protofile) Free(cur_func->protofile);
  339.        DeleteStringList2(&cur_func->args,1);
  340.        if(cur_func->cret)      Free(cur_func->cret);
  341.        DeleteStringList(&cur_func->calls);
  342.        DeleteStringList(&cur_func->called);
  343.        DeleteStringList(&cur_func->used);
  344.        DeleteStringList(&cur_func->v_refs);
  345.        DeleteStringList(&cur_func->f_refs);
  346.        Free(cur_func);
  347.       }
  348.  
  349.     cur_func=NULL;
  350.    }
  351. }
  352.